package main

import "fmt"

func main() {
	fmt.Println(plusOne([]int{4, 3, 2, 1}))

	l1 := &ListNode{
		Val: 1,
		Next: &ListNode{
			Val: 2,
			Next: &ListNode{
				Val:  4,
				Next: nil,
			},
		},
	}

	l2 := &ListNode{
		Val: 1,
		Next: &ListNode{
			Val: 3,
			Next: &ListNode{
				Val:  4,
				Next: nil,
			},
		},
	}
	result := mergeTwoLists(l1, l2)
	fmt.Printf("%#v\n", result)
}

// plusOne 66. 加一
// link https://leetcode.cn/problems/plus-one/
func plusOne(digits []int) []int {

	for i := len(digits) - 1; i >= 0; i-- {
		digits[i]++
		if digits[i] != 10 {
			return digits
		}
		digits[i] = 0
	}

	return append([]int{1}, digits...)

}

type ListNode struct {
	Val  int
	Next *ListNode
}

// mergeTwoLists 21. 合并两个有序链表
// link https://leetcode.cn/problems/merge-two-sorted-lists/
func mergeTwoLists(list1 *ListNode, list2 *ListNode) *ListNode {
	prev := &ListNode{}
	list := prev

	for list1 != nil && list2 != nil {
		if list1.Val >= list2.Val {
			prev.Next = list2
			list2 = list2.Next
		} else {
			prev.Next = list1
			list1 = list1.Next
		}
		prev = prev.Next
	}

	if list1 != nil {
		prev = list1
	}

	if list2 != nil {
		prev = list2
	}

	return list.Next

}
